home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13677 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.3 KB

  1. Path: camelot.ccs.neu.edu!will
  2. From: will@ccs.neu.edu (William D Clinger)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: C compilers which optimize tail calls (was: GOTO controversy)
  5. Date: 9 Apr 1996 18:43:16 GMT
  6. Organization: College of CS, Northeastern University
  7. Message-ID: <4keb44$86h@camelot.ccs.neu.edu>
  8. References: <oun34tm3c7.fsf@lynx.cs.byu.edu> <DpCv8o.9Mw@ida.liu.se> <ouivfcnaxq.fsf@lynx.cs.byu.edu>
  9. NNTP-Posting-Host: krakatoa.ccs.neu.edu
  10.  
  11. In article <ouivfcnaxq.fsf@lynx.cs.byu.edu>
  12. hall@cs.byu.edu (Kelly Hall) writes:
  13. >    Rickard> All in all, I don't think it's wise to assume that tail
  14. >    Rickard> call optimization is done by the typical C compiler.
  15. >
  16. >I stand corrected.  So why haven't more C compiler folks added this
  17. >useful optimization?
  18.  
  19. Stack allocation of mutable variables and data often
  20. interferes with proper tail recursion.  Consider
  21.  
  22. int f (int n) {
  23.     int A[10];
  24.     int i;
  25.     for (i = 0; i < 10; i = i + 1)
  26.         A[i] = n * i;
  27.     return g (&n, &(A[0]));
  28. }
  29.  
  30. Yes, it is possible to write a correct C compiler that
  31. compiles the tail-recursive call to g as a tail recursion.
  32. The compiler technology required to do this, however, would
  33. be considered bizarre (or even broken) by most C programmers.
  34.  
  35. I very much doubt whether any production C compiler
  36. can be relied upon to generate properly tail recursive code.
  37.  
  38. William D Clinger
  39.